package com.yyk.yykywd.mvvm.ui

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.yyk.yykywd.*
import com.yyk.yykywd.base.BaseActivity
import com.yyk.yykywd.mvvm.adapter.ReturnMoneyAdapter
import com.yyk.yykywd.mvvm.view_model.ReturnMoneyViewModel
import com.yyk.yykywd.util.ToAnotherActivity
import kotlinx.android.synthetic.main.activity_return_money.*
import kotlinx.android.synthetic.main.activity_return_money.loading_layout
import kotlinx.android.synthetic.main.layout_load.*
import kotlin.reflect.typeOf

class ReturnMoneyActivity : BaseActivity() {
    private lateinit var viewModel: ReturnMoneyViewModel
    private lateinit var adapter: ReturnMoneyAdapter

    override fun initView() {
        setBackArrow()
        loading_layout.showLoading()

        viewModel = ViewModelProviders.of(this).get(ReturnMoneyViewModel::class.java)
        viewModel.type = intent.getStringExtra(TYPE)

        if (viewModel.type == FROM_RETURN_MONEY_AGENT) {
            setTitle("回款完成")
        } else {
            setTitle("安装医院")
        }

        rv.layoutManager = LinearLayoutManager(mActivity)
        adapter = ReturnMoneyAdapter(mActivity, viewModel.type, R.layout.item_return_money, viewModel.returns)
        rv.adapter = adapter
    }

    override fun setListener() {
        adapter.setOnItemClickListener { _, _, position ->
            with(viewModel.returns[position]) {
                val showType = show_type ?: 0
                if (viewModel.type == FROM_RETURN_MONEY_AGENT) {
                    ToAnotherActivity.toDetailActivity(mActivity,
                            viewModel.type,
                            id.toString(),showType)
                } else {
                    ToAnotherActivity.toReturnMoneyDetailActivity(mActivity,
                            viewModel.type,
                            id.toString(), 0, showType, type_s)
                }
            }
        }

        viewModel.fullContract.observe(this, Observer {
            hideRefreshAndLoadMore(srl)
            showStateLayout(adapter, it.code, viewModel.returns, it.msg, loading_layout)
        })

        viewModel.error.observe(this, Observer {
            setLoadingLayoutError(loading_layout, mActivity)
        })

        srl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.loadMore()
                viewModel.getReturnMoneyList()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                reloadData()
            }

        })
    }

    override fun initData() {
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_return_money
    }

    override fun onResume() {
        super.onResume()
        reloadData()
    }


    private fun reloadData() {
        viewModel.refresh(viewModel.returns)
        viewModel.getReturnMoneyList()
    }
}
